<?php

namespace app\admin\controller\factor;

use app\common\controller\Backend;
use ReflectionFunction;
use think\Db;

/**
 * 碳排放因子库核算认证
 *
 * @icon fa fa-circle-o
 */
class AccountAuthentication extends Backend
{

    /**
     * AccountAuthentication模型对象
     * @var \app\admin\model\factor\AccountAuthentication
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\factor\AccountAuthentication;
        $statusList = $this->model->getStatusList();
        $this->view->assign("statusList", $statusList);
        $qMethodList = $this->model->getQuantificationMethodList();
        $this->view->assign("qMethodList", $qMethodList);
        $this->assignConfig("qMethodList", $qMethodList);
        $this->assignConfig("statusList", $statusList);
        $bodyName = \app\admin\model\factor\CertificationBody::column('body_name', 'id');
        $this->assignConfig("bodyName", $bodyName);
        $this->view->assign("bodyName", $bodyName);
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        $list->each(function ($row) {
            $row->appendRelationAttr('factor', ['factor_name' => 'name']);
            $row->appendRelationAttr('userInfo', ['username' => 'nickname']);
//            $row->append(['full_address']);
        });
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);

    }

    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                $now = time();
                if ($params['status']) {
                    if ($params['status'] == 2) {
                        if (!$params['reason']) {
                            $this->error('请填写驳回原因');
                        }
                        $row->reason = $params['reason'];
                    }
                    $row->status = $params['status'];
                    $row->checktime = $now;
                } else {
                    $row->checktime = null;
                }
                Db::startTrans();
                try {
                    if ($row->status == 1) {

                        $row->factor()->update(['is_account' => $row->status, 'account_time' => $now]);
                    } else {
                     $row->factor()->update(['is_account' => $row->status, 'account_time' => $now]);
                    }
                    $row->save();
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error();
        }
//        $row->product_image_str = implode(',', $row->product_image);
        $row->appendRelationAttr('factor', ['factor_name' => 'name']);
        $row->appendRelationAttr('userInfo', ['username' => 'nickname']);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
